/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package webSiteDataStore;

import java.sql.*;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.sql.DataSource;
import javax.naming.InitialContext;


public class ConnectionPool
{
    private static final Logger theLogger = Logger.getLogger(ConnectionPool.class.getName());
    private static ConnectionPool pool = null;
    private static DataSource dataSource = null;

    private ConnectionPool()
    {
        try
        {
            InitialContext ic = new InitialContext();
            dataSource = (DataSource)ic.lookup("java:/comp/env/jdbc/WebSiteDataStore");
        }
        catch(Exception e)
        {
            theLogger.log(Level.SEVERE, null, e);
        }
    }

    public static ConnectionPool createInstance()
    {
        if (pool == null)
        {
            pool = new ConnectionPool();
        }
        return pool;
    }

    public static Connection getConnection() throws SQLException
    {
        return dataSource.getConnection();
    }

    public static void freeConnection(Connection connection)
    {
         SafeFreeResource.closeConnection(connection);
    }
}
